Test: add unit tests for VetService (business logic and error handling)#185
Conversation
* Added unit tests for createVet covering: * successful creation * user not found * duplicate licenseId * user already registered as vet * Verified role update from OWNER to VET during vet creation * Added tests for getAllVets and getVetById * Replaced mocked entities with real objects to support DTO mapping * Ensured proper exception handling with ResourceNotFoundException and BusinessRuleException * Used Mockito for repository mocking and interaction verification
📝 WalkthroughWalkthroughAdded a new JUnit 5 test class Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/test/java/org/example/vet1177/services/VetServiceTest.java (2)
122-169: Consider extracting shared fixtures forUser/Vet.
UserandVetsetup is duplicated across retrieval tests; small builders/helpers would reduce noise and future edit cost.Example refactor
+ private User buildActiveUser(String name, String email) { + User user = new User(); + user.setName(name); + user.setEmail(email); + user.setActive(true); + return user; + } + + private Vet buildVet(User user, String licenseId) { + Vet vet = new Vet(); + vet.setUser(user); + vet.setLicenseId(licenseId); + return vet; + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/example/vet1177/services/VetServiceTest.java` around lines 122 - 169, Extract the duplicated User/Vet setup in VetServiceTest into small private factory/helper methods (e.g., private User makeUser(String name, String email, boolean active) and private Vet makeVet(User user, String licenseId)) and update tests should_return_all_vets and should_return_vet_by_id to call these helpers instead of duplicating object creation; ensure the helpers set the same fields currently used in tests (name/email/active on User and user/licenseId on Vet) and keep existing mock interactions (when(vetRepository.findAll())... and when(vetRepository.findById(id))... ) and assertions unchanged.
39-116: Good createVet coverage overall; one branch is still missing.You covered success + key business-rule failures well. The remaining gap is the repository-save failure path in
createVetthat should still surface asBusinessRuleException.Add a focused test for the save-failure translation path
+ `@Test` + void should_throw_business_rule_when_persistence_fails_on_save() { + UUID userId = UUID.randomUUID(); + User user = new User(); + user.setRole(Role.OWNER); + + VetRequest request = new VetRequest(userId, "LIC123", "Surgery", "Info"); + + when(userRepository.findById(userId)).thenReturn(Optional.of(user)); + when(vetRepository.existsByLicenseId("LIC123")).thenReturn(false); + when(vetRepository.existsById(userId)).thenReturn(false); + when(vetRepository.save(any(Vet.class))) + .thenThrow(new org.springframework.dao.DataIntegrityViolationException("constraint")); + + assertThrows(BusinessRuleException.class, () -> vetService.createVet(request)); + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/example/vet1177/services/VetServiceTest.java` around lines 39 - 116, Add a unit test in VetServiceTest that covers the repo-save failure path: arrange a valid User (role OWNER) and a VetRequest, mock userRepository.findById(...) to return the user, mock vetRepository.existsByLicenseId(...) and existsById(...) to return false, then stub vetRepository.save(...) to throw a RuntimeException (or generic exception) and assert that calling vetService.createVet(request) throws BusinessRuleException; reference createVet, vetRepository.save, and BusinessRuleException to locate the code under test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/test/java/org/example/vet1177/services/VetServiceTest.java`:
- Around line 122-169: Extract the duplicated User/Vet setup in VetServiceTest
into small private factory/helper methods (e.g., private User makeUser(String
name, String email, boolean active) and private Vet makeVet(User user, String
licenseId)) and update tests should_return_all_vets and should_return_vet_by_id
to call these helpers instead of duplicating object creation; ensure the helpers
set the same fields currently used in tests (name/email/active on User and
user/licenseId on Vet) and keep existing mock interactions
(when(vetRepository.findAll())... and when(vetRepository.findById(id))... ) and
assertions unchanged.
- Around line 39-116: Add a unit test in VetServiceTest that covers the
repo-save failure path: arrange a valid User (role OWNER) and a VetRequest, mock
userRepository.findById(...) to return the user, mock
vetRepository.existsByLicenseId(...) and existsById(...) to return false, then
stub vetRepository.save(...) to throw a RuntimeException (or generic exception)
and assert that calling vetService.createVet(request) throws
BusinessRuleException; reference createVet, vetRepository.save, and
BusinessRuleException to locate the code under test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9ab44342-a053-43eb-9ef1-942d923258b1
📒 Files selected for processing (1)
src/test/java/org/example/vet1177/services/VetServiceTest.java
Added unit tests for createVet covering:
Summary by CodeRabbit